<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 站点全局配置 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\model;


use think\Cache;
use think\Db;

class SiteConfig {
    const ERR_UNKNOWN_CONFIG_NAME = -1;
    const ERR_UNKNOWN_CONFIG_VALUE = -2;

    const SITE_NAME = 'siteName';
    const SITE_SLOGAN = 'siteSlogan';
    const SITE_KEYWORD = 'siteKeyword';
    const SITE_DESC = 'siteDesc';
    const FAVICON = 'favicon';
    const LOGO = 'logo';
    const SLOGO = 'slogo';
    const LOGIN_BK = 'loginBk';
    const ICP = 'icp';
    const STAT = 'stat';
    const COPYRIGHT = 'copyright';
    const THEME = 'theme';
    const WX_APPID = 'wxAppid';
    const WX_APPSECRET = 'wxAppsecret';
    const QINIU_AK = 'qiniuAK';
    const QINIU_SK = 'qiniuSK';
    const QINIU_BUCKET = 'qiniuBucket';
    const QINIU_DOMAIN = 'qiniuDomain';
    const DEBUG = 'debug';

    /**
     * 允许的配置列表
     *
     * @var array
     */
    public static $cfs = [
        self::SITE_NAME => [
            'title' => '站点名称',
            'defaultVal' => 'DingZone',
            'group' => '站点信息'
        ],
        self::SITE_SLOGAN => [
            'title' => '站点Slogan',
            'defaultVal' => '钉钉极速开发框架',
            'group' => '站点信息'
        ],
        self::SITE_KEYWORD => [
            'title' => 'keywords',
            'defaultVal' => 'DingZone',
            'group' => '站点信息'
        ],
        self::SITE_DESC => [
            'title' => 'description',
            'defaultVal' => 'DingZone',
            'group' => '站点信息'
        ],
        self::FAVICON => [
            'title' => 'favicon',
            'defaultVal' => '',
            'group' => '站点信息',
            'type' => 'image',
            'width' => '20px',
            'height' => '20px'
        ],
        self::LOGO => [
            'title' => 'Logo',
            'defaultVal' => '/static/images/logo.png',
            'group' => '站点信息',
            'type' => 'image',
            'width' => '200px',
            'height' => '60px'
        ],
        self::SLOGO => [
            'title' => '小Logo',
            'defaultVal' => '/static/images/slogo.png',
            'group' => '站点信息',
            'type' => 'image',
            'width' => '60px',
            'height' => '60px'
        ],
        self::LOGIN_BK => [
            'title' => '登录页背景',
            'defaultVal' => '',
            'group' => '站点信息',
            'type' => 'image',
            'width' => '192px',
            'height' => '80px',
            'tip' => '最佳尺寸：1920px * 800px'
        ],
        self::ICP => [
            'title' => '备案号',
            'defaultVal' => '',
            'group' => '站点信息'
        ],
        self::STAT => [
            'title' => '统计代码',
            'defaultVal' => '',
            'group' => '站点信息',
            'type' => 'textarea'
        ],
        self::COPYRIGHT => [
            'title' => 'Copyright',
            'defaultVal' => '',
            'group' => '站点信息',
            'type' => 'textarea'
        ],
        self::THEME => [
            'title' => '风格',
            'defaultVal' => '',
            'options' => ['' => '默认', 'red' => '红色', 'blue' => '蓝色'],
            'group' => '站点风格',
            'type' => 'radio'
        ],
        self::WX_APPID => [
            'title' => 'Appid',
            'defaultVal' => '',
            'group' => '微信公众号'
        ],
        self::WX_APPSECRET => [
            'title' => 'Appsecret',
            'defaultVal' => '',
            'group' => '微信公众号'
        ],
        self::QINIU_AK => [
            'title' => 'accessKey',
            'defaultVal' => '',
            'group' => '七牛云存储'
        ],
        self::QINIU_SK => [
            'title' => 'secretKey',
            'defaultVal' => '',
            'group' => '七牛云存储'
        ],
        self::QINIU_BUCKET => [
            'title' => 'bucket',
            'defaultVal' => '',
            'group' => '七牛云存储'
        ],
        self::QINIU_DOMAIN => [
            'title' => '域名',
            'defaultVal' => '',
            'group' => '七牛云存储'
        ],
        self::DEBUG => [
            'title' => '调试模式',
            'defaultVal' => 'no',
            'options' => ['yes' => '打开', 'no' => '关闭'],
            'group' => '调试信息',
            'type' => 'radio'
        ]
    ];

    /**
     * 获取缓存key对象
     *
     * @return CacheKey
     */
    private static function _getCacheKey() {
        return CacheKey::module("core")->type("SiteConfig");
    }

    /**
     * 获取配置
     *
     * @param string $name
     * @return array|bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function get($name = "") {
        $cacheKey = self::_getCacheKey();
        $configs = $cacheKey->getValue();
        if (!$configs) {
            $configs = self::reload();
        }
        if (empty($name)) {
            return $configs;
        }
        return $configs[$name];
    }

    /**
     * 更新配置
     *
     * @param $name
     * @param $value
     * @param bool $reload
     * @return int|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function set($name, $value, $reload = true) {
        if (!isset(self::$cfs[$name])) {
            // 设置项不在白名单之内
            return self::ERR_UNKNOWN_CONFIG_NAME;
        }
        $cf = self::$cfs[$name];
        if (isset($cf['options']) && is_array($cf['options']) && !array_key_exists($value, $cf['options'])) {
            // 设置的值错误
            return self::ERR_UNKNOWN_CONFIG_VALUE;
        }
        $row = Db::name("SiteConfig")->where(['name' => $name])->find();
        if (empty($row)) {
            $ret = Db::name("SiteConfig")->insert([
                'name' => $name,
                'value' => $value
            ]);
        } else {
            $ret = Db::name("SiteConfig")->where(['name' => $name])->update([
                'value' => $value
            ]);
        }
        if ($reload) {
            self::reload();
        }
        return $ret;
    }

    /**
     * 重新加载配置
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function reload() {
        $rows = Db::name("SiteConfig")->select();
        $configs = [];
        if ($rows) {
            foreach ($rows as $row) {
                $configs[$row['name']] = $row['value'];
            }
        }
        foreach (self::$cfs as $name => $cf) {
            if (!isset($configs[$name])) {
                $configs[$name] = $cf['defaultVal'];
            }
        }
        $cacheKey = self::_getCacheKey();
        $cacheKey->setValue($configs);
        return $configs;
    }
}